package com.ljt.yupictureddd.application.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljt.yupictureddd.domain.space.service.SpaceDomainService;
import com.ljt.yupictureddd.infrastracture.exception.ErrorCode;
import com.ljt.yupictureddd.infrastracture.exception.ThrowUtils;
import com.ljt.yupictureddd.infrastracture.mapper.SpaceMapper;
import com.ljt.yupictureddd.interfaces.dto.space.SpaceAddRequest;
import com.ljt.yupictureddd.interfaces.dto.space.SpaceQueryRequest;
import com.ljt.yupictureddd.domain.space.entity.Space;
import com.ljt.yupictureddd.domain.user.entity.User;
import com.ljt.yupictureddd.interfaces.vo.space.SpaceVO;
import com.ljt.yupictureddd.application.service.SpaceApplicationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 六件套
 * @description 针对表【space(空间)】的数据库操作Service实现
 * @createDate 2025-03-14 11:06:32
 */
@Service
public class SpaceApplicationServiceImpl extends ServiceImpl<SpaceMapper, Space>
        implements SpaceApplicationService {

    @Resource
    private SpaceDomainService spaceDomainService;
    // @Autowired
    // @Lazy
    // 为了方便部署，不使用
    // private DynamicShardingManager dynamicShardingManager;

    /**
     * 创建空间
     *
     * @param request
     * @return
     */
    @Override
    public long createSpace(SpaceAddRequest request, User loginUser) {
        return spaceDomainService.createSpace(request,loginUser);
    }

    @Override
    public QueryWrapper<Space> getQueryWrapper(SpaceQueryRequest request) {
        return spaceDomainService.getQueryWrapper(request);
    }

    @Override
    public SpaceVO getSpaceVO(Space space, HttpServletRequest request) {
        return spaceDomainService.getSpaceVO(space, request);
    }

    @Override
    public Page<SpaceVO> getSpaceVOList(Page<Space> page, HttpServletRequest request) {
       return spaceDomainService.getSpaceVOList(page, request);
    }

    @Override
    public void checkSpaceAuth(User loginUser, Space space) {
        ThrowUtils.throwIf(!ObjUtil.equals(loginUser.getId(), space.getUserId()) || !loginUser.isAdmin(), ErrorCode.NO_AUTH_ERROR);
    }
}




